Tahap Awal

Menginstall package EBImage

Langkah ini adalah langkah yang wajib dilakukan agar fungsi dari EBImage itu dapat diakses dengan R Studio.

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
## Bioconductor version '3.10' is out-of-date; the current release version '3.12'
##   is available with R version '4.0'; see https://bioconductor.org/install
BiocManager::install("EBImage")
## Bioconductor version 3.10 (BiocManager 1.30.12), R 3.6.2 (2019-12-12)
## Installing package(s) 'EBImage'
## package 'EBImage' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpawKIs9\downloaded_packages
## Installation paths not writeable, unable to update packages
##   path: C:/Program Files/R/R-3.6.2/library
##   packages:
##     boot, class, cluster, codetools, KernSmooth, lattice, MASS, Matrix, mgcv,
##     nlme, nnet, spatial, survival

Tahap Persiapan

Men-set lokasi kerja, memanggil package EBImage, reading image

Set Lokasi

Tahap ini adalah tahap dimana kita mengatur lokasi tempat gambar yang akan dieksekusi berada (biasanya satu folder dalam file project).

#set lokasi kerja
setwd("C:/Users/DELL/Documents/R/EBImage")

Memanggil EBImage

Memanggil package yang sudah diinstall di tahap 1, yaitu package EBImage.

#memanggi package
library(EBImage)

Reading Image

Dan yang terakhir, kita perlu melakukan assign (import)pada fungsi readImage dengan judul photo yang sudah ditaruh di folder set kerja dan memberinya nama misal Image.

Image <- readImage('Greenview.jpg')

Tahap Percobaan

Menampilkan Gambar, Sifat Gambar, dan Histogram Gambar

Menampilkan Gambar

Disini adalah cara untuk menampilkan file asli dari gambar yang sudah diimport ke dalam project kita.

display(Image) 

Sifat Gambar

Disini adalah cara untuk menampilkan sifat dari gambar yang sudah diimport. Adapun sifat dari gambar itu berupa spesifikasi gambar dan spesifikasi berupa matriks.

print(Image)
## Image 
##   colorMode    : Color 
##   storage.mode : double 
##   dim          : 2560 1440 3 
##   frames.total : 3 
##   frames.render: 1 
## 
## imageData(object)[1:5,1:6,1]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [2,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [3,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [4,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [5,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843

Histogram Gambar

Histogram gamabr adalah suatu grafik yang akan menahistmpilkan banyak hal bisa berupa kecerahan (brightness), kontas (contrast), warna (color) dari sebuah gambar.

hist(Image)

Tahap Akhir

Tahap ini adalah tahap dimana kita melakukan pengolahan terhadap gambar agar menghasilkan output yang diinginkan.

Brightness

Image1 <- Image + 0.2
Image2 <- Image - 0.2
par(mfrow= c(1,2))
plot(Image1)
plot(Image2)

Adjusting Contrast

Image3 <- Image * 0.5
Image4 <- Image * 2
par(mfrow= c(1,2))
plot(Image3)
plot(Image4)

Gamma Correction

Image5 <- Image ^ 2
Image6 <- Image ^ 0.7
par(mfrow= c(1,2))
plot(Image5)
plot(Image6)

Cropping

display(Image[2560:1440, 700:500,])

Spatial Transformation

Imagetr <- translate(rotate(Image, 45), c(50, 0))
display(Imagetr)

Color Management

colorMode(Image) <- Grayscale
display(Image)
## Only the first frame of the image stack is displayed.
## To display all frames use 'all = TRUE'.

print(Image)
## Image 
##   colorMode    : Grayscale 
##   storage.mode : double 
##   dim          : 2560 1440 3 
##   frames.total : 3 
##   frames.render: 3 
## 
## imageData(object)[1:5,1:6,1]
##           [,1]      [,2]      [,3]      [,4]      [,5]      [,6]
## [1,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [2,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [3,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [4,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
## [5,] 0.1568627 0.1568627 0.1568627 0.1607843 0.1607843 0.1607843
colorMode(Image) <- Color
display(Image)

Filtering

Contoh 1

fHigh <- matrix(1, nc = 3, nr = 3)
fHigh[2, 2] <- -8
Image.fHigh <- filter2(Image, fHigh)
display(Image.fHigh)

Contoh 2

fLow <- makeBrush(21, shape= 'disc', step=FALSE)^2
fLow <- fLow/sum(fLow)
Image.fLow <- filter2(Image, fLow)
display(Image.fLow)